package com.example.demo.utils;
import java.util.ArrayList;
import java.util.Date;

import com.example.demo.bean.Dept;
import com.example.demo.bean.User;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @author lyc
 * @date 2018/9/29.
 * json-util
 *
 * https://github.com/duanxinyuan/Json-Utils
 * https://gitee.com/yidasanqian/json-util
 * https://gitee.com/xxssyyyyssxx/json-common  这个思想可以
 *
 * https://github.com/lyc88/serialization 序列化
 */
@Slf4j
public class JsonUtlis {
    private static ObjectMapper objectMapper = new ObjectMapper();
    static {
        // 全部字段序列化
        //对象的所有字段全部列入
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        //取消默认转换timestamps形式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        //所有的日期格式都统一为以下的样式，即yyyy-MM-dd HH:mm:ss
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        //忽略空Bean转json的错误
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
        //忽略 在json字符串中存在，但是在java对象中不存在对应属性的情况。防止错误
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
    }

    public  static <T> String classToString(T t){
        try {
            return objectMapper.writeValueAsString(t);
        } catch (JsonGenerationException e) {
            log.error("error:{}",e);
        } catch (JsonMappingException e) {
            log.error("error:{}",e);
        } catch (IOException e) {
            log.error("error:{}",e);
        }
        return null;
    }

    /**
     * Json字符串转换成实体对象
     * @param json
     * @param clazz 实体对象所属类Class
     * @return
     */
    public  static <T> T jsonToClass(String json, Class<T> clazz){
        try {
            return objectMapper.readValue(json, clazz);
        } catch (JsonGenerationException e) {
            log.error("error:{}",e);
        } catch (JsonMappingException e) {
            log.error("error:{}",e);
        } catch (IOException e) {
            log.error("error:{}",e);
        }
        return null;
    }

    public static void main(String[] args) {
        User dept = new User();
        dept.setId(0);
        dept.setName("aaa");
        dept.setAge(0);
        dept.setPthone("aa");
        dept.setEmail("aaa");
        dept.setDate(new Date());

        String json = classToString(dept);
        //对象
        System.out.println(json);
        System.out.println(jsonToClass(json,User.class));
        //map
        Map map = jsonToClass(json,Map.class);
        System.out.println(map);

        User user = new User();
        user.setId(0);
        user.setName("aaa");
        user.setAge(0);
        user.setPthone("aa");
        user.setEmail("aaa");
        user.setDate(new Date());

        //集合
        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(dept);
        String userss = classToString(users);
        System.out.println(userss);
        List<User> list = jsonToClass(userss,List.class);
        System.out.println(list);

    }
}